﻿/*************************************************************************
	Copyright (C) 2009-2010 欣风工作室    
	All rights reserved
	
	CLR版本:			2.0.50727.4952
	文件名:				clsAutoCloseWindow
	创建时间:			2011/6/6 0:44:19
	作者:				我烤
	QQ:					8230707
	E-Mail:				xfgzskof@163.com
	
	修改日志:   肥总无敌
	
*************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace 街头篮球自动购买道具
{
    /// <summary>
    ///测试成功q s 
    /// </summary>
    public class AutoCloseWindow
    {
        /// <summary>
        /// 点击次数
        /// </summary>
        public static int AppButtonClickCount;
        /// <summary>
        /// 弹出式窗口标题
        /// </summary>
        public static string WindowTitle;

        private delegate bool CallBack(int hwnd, int lParam);
        [DllImport("user32.dll")]
        private static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("user32.dll")]
        private static extern int FindWindow(string strclassName, string strWindowName);
        [DllImport("user32.dll")]
        private static extern int GetLastActivePopup(int hWnd);
        [DllImport("user32.dll")]
        private static extern int AnyPopup();
        [DllImport("user32.dll")]
        private static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll")]
        private static extern int EnumThreadWindows(int dwThreadId, CallBack lpfn, int lParam);
        [DllImport("user32.dll")]
        private static extern int EnumWindows(CallBack lpfn, int lParam);
        [DllImport("user32.dll")]
        private static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);
        [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern int SendMessage(int hwnd, uint wMsg, int wParam, int lParam);

        /// <summary>
        /// 获取指定窗口的弹出式窗口
        /// </summary>
        /// <param name="winName"></param>
        /// <returns>窗体柄句</returns>
        public static int GetPoPupWindowhWndByName(string winName)
        {
            // 取得指定窗口柄句
            int result = FindWindow(null, winName);

            if (result != 0)
            {
                // 获取弹出窗口
                result = GetLastActivePopup(result);

                if (result != 0)
                {
                    // 获取窗体名称
                    StringBuilder sb = new StringBuilder(512);
                    GetWindowText(result, sb, sb.Capacity);
                    WindowTitle = sb.ToString();
                    Console.WriteLine(WindowTitle);
                }
            }

            return result;
        }

        /// <summary>
        /// 获取指定窗体的子窗体
        /// </summary>
        /// <param name="hWnd">父窗体柄句</param>
        /// <param name="winName">子窗体题材</param>
        /// <returns>窗体柄句</returns>
        public static int GetChildWindowByName(int hWnd, string winName)
        {
            int result = FindWindowEx(hWnd, 0, null, "vshost.exe － 安全警告");
            return result;
        }

        /// <summary>
        /// 模拟点击弹出式窗口的指定按钮
        /// </summary>
        /// <param name="hWnd">弹出式窗口柄句</param>
        /// <param name="buttonName">按钮名</param>
        /// <returns></returns>
        public static bool DoClick2PopupWindow(int hWnd, string buttonName)
        {
            const uint BM_CLICK = 0xF5; // 鼠标点击的消息
            bool result = false;

            // 获取要操作的按钮
            hWnd = FindWindowEx(hWnd, 0, null, buttonName);
            if (hWnd != 0)
            {
                // 模拟鼠标点击
                SendMessage(hWnd, BM_CLICK, 0, 0);
                // 累加点击次数
                AppButtonClickCount++;
                Console.WriteLine(AppButtonClickCount.ToString());
                result = true;
            }
            return result;
        }
    }
}
